home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 615 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  74 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: beginner question - typecasting
  5. Date: 04 Jan 1996 22:33:57 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan4173357@g7240065.bridge.bst.bls.com>
  8. References: <4cei1r$s02@sun.cis.smu.edu> <30EBCED7.774@sto.fdata.se>
  9.     <4che01$kjm@colossus.holonet.net>
  10. NNTP-Posting-Host: bstfirewall.bst.bls.com
  11. In-reply-to: russell@news.mdli.com's message of 4 Jan 1996 20:40:33 GMT
  12.  
  13. Russell Blackadar (russell@news.mdli.com) wrote:
  14. : Niklas Mellin (niklas.mellin@sto.fdata.se) wrote:
  15. :: Damon Bowman wrote:
  16.  
  17. ::> When you are typecasting, is there any difference between:
  18.  
  19. ::> a = int(x)
  20. ::> and
  21. ::> a = (int) x
  22. :: [...]
  23. : [...]
  24.  
  25. :: The 2nd form is an inheritance from plain C, and doesn't work
  26. :: when x is of class type...
  27.  
  28. : Ahem, this is wrong.  Both forms are valid in C++, and both
  29. : invoke the class's operator int.  There is absolutely no
  30. : functional difference between the two forms.  It would be
  31. : nice to have just one syntax to convert types, but since C
  32. : supports both, C++ must also.  Take a look at the ARM, please.
  33.  
  34. C does not support both !
  35. The first form was introduced by C++.
  36.  
  37. As Russell states there is absolutely no functional difference between the
  38. two forms - but should there be ?
  39.  
  40. Example:
  41.  
  42. class A
  43. {
  44.   public:
  45.     ...
  46.     A(const B& b);
  47.     ...
  48. };
  49.  
  50. class B
  51. {
  52.   public:
  53.     ...
  54.     operator A (void);
  55.     ...
  56. };
  57.  
  58. Couldn't the different syntax be used to disambiguate this problem:
  59.  
  60.    a = A(b);    // should call A constructor.
  61.    a = (A)b;    // should call B type operator.
  62.  
  63. Instead of
  64.  
  65.    a = A(b);    // Error: 2 possible conversion for b;
  66.    a = (A)b;    // Error: 2 possible conversion for b;
  67.  
  68. Just idle thoughts
  69. Regards
  70.  
  71.    -A.
  72. -- 
  73. | A.Champion                |
  74.